package entity.User.dao.impl;

import entity.User.dao.UserDAO;
import entity.User.model.User;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * Created by vanches on 28.12.14.
 */
public class UserDaoImpl extends HibernateDaoSupport implements UserDAO {
    @Override
    public List getAllUser() {
        return  getHibernateTemplate().find("from User ");
    }

    @Override
    public void save(User user) {
        getHibernateTemplate().save(user);
    }

    @Override
    public void update(User user) {
        getHibernateTemplate().update(user);
    }

    @Override
    public void delete(User user) {
        getHibernateTemplate().delete(user);
    }

    @Override
    public boolean checkLogin(String login) {
        List list = getHibernateTemplate().find("from User where login=?", login);
        if (list.size() == 0) {
            return false;
        }
        else
        {
            return true;
        }

    }

    @Override
    public User getUserById(int id) {
            return (User)getHibernateTemplate().find("from User where id="+id).get(0);

    }

    @Override
    public User login(String login, String pass) {
        List list = getHibernateTemplate().find("from User where login='"+login+"'"+" and pass='"+pass+"'");
        if (list.size() !=0) {
            return (User) list.get(0);
        }
        else
        {
            return null;
        }
    }
}
